﻿using System.Globalization;
using System.Threading;
using System.Web.Mvc;
using POS.API.Helpers;
using POS.Constants;

namespace POS.Controllers
{

    public abstract class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            CultureInfo.CurrentCulture.ClearCachedData();
            var currentCulture = CultureInfo.CreateSpecificCulture("en-ZA");
            currentCulture.NumberFormat.CurrencyDecimalSeparator = ".";
            currentCulture.NumberFormat.CurrencyGroupSeparator = ",";
            currentCulture.NumberFormat.NumberDecimalSeparator = ".";
            currentCulture.NumberFormat.NumberGroupSeparator = ",";
            currentCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            currentCulture.DateTimeFormat.LongDatePattern = "d/m/Y";
            currentCulture.DateTimeFormat.DateSeparator = "/";
            Thread.CurrentThread.CurrentCulture = currentCulture;
            Thread.CurrentThread.CurrentUICulture = currentCulture;
        }

        public int WaiterId
        {
            get { return ClaimsHelper.RetrieveClaim<int>(PosIdentityClaimConstants.WaiterId); }
        }

        //public int CompanyId
        //{
        //    get { return ClaimsHelper.RetrieveClaim<int>(IdentityClaimConstants.CompanyId); }
        //}

        //public string CustomerName
        //{
        //    get { return ClaimsHelper.RetrieveClaim<string>(ClientPortalIdentityClaimConstants.CustomerName); ; }
        //}

        //public string CompanyName
        //{
        //    get { return ClaimsHelper.RetrieveClaim<string>(ClientPortalIdentityClaimConstants.CompanyName); }
        //}

        //public int CurrencyDecimalDigits
        //{
        //    get { return ClaimsHelper.RetrieveClaim<int>(IdentityClaimConstants.CurrencyDecimalDigits); }
        //}

        //public int NumberDecimalDigits
        //{
        //    get { return ClaimsHelper.RetrieveClaim<int>(IdentityClaimConstants.NumberDecimalDigits); }
        //}

        //public string CurrencySymbol
        //{
        //    get { return ClaimsHelper.RetrieveClaim<string>(IdentityClaimConstants.CurrencySymbol); }
        //}

        //[HttpGet]
        //[OutputCache(Duration = 3600, VaryByParam = "id", Location = OutputCacheLocation.ServerAndClient)]
        //public virtual ActionResult GetImage(string id)
        //{
        //    var noImg = new Bitmap(1, 1);
        //    noImg.SetPixel(0, 0, Color.White);

        //    return File(ImageHelper.ResizeImage(Globals.CurrentCompanyLogo ?? ImageHelper.ImageToByteArray(noImg), 190, 95), "image/jpg", "logo.jpg");
        //}

        //protected override void OnException(ExceptionContext filterContext)
        //{
        //    //TODO: Log, email etc.
        //    if (filterContext.HttpContext.IsCustomErrorEnabled)
        //    {
        //        filterContext.ExceptionHandled = true;

        //        if (filterContext.Exception is UnauthorizedAccessException)
        //        {
        //            this.View("ErrorUnauthorised").ExecuteResult(this.ControllerContext);
        //        }
        //                        else if (filterContext.Exception is InvalidLinkException)
        //        {
        //                                ExceptionHandler.LogException(filterContext.HttpContext.ApplicationInstance.Context, filterContext.Exception, System.Configuration.ConfigurationManager.AppSettings["ErrorNotificationAddress"], false);
        //            this.View("ErrorInvalidLink").ExecuteResult(this.ControllerContext);
        //        }
        //        else if (filterContext.Exception is ExternalSessionExpiredException)
        //        {
        //            this.View("SessionExpired").ExecuteResult(this.ControllerContext);
        //        }
        //        else if (filterContext.Exception is ExternalAccessRevokedException)
        //        {
        //            this.View("ErrorSupplierDisabled").ExecuteResult(this.ControllerContext);
        //        }
        //        else
        //        {
        //                                ExceptionHandler.LogException(filterContext.HttpContext.ApplicationInstance.Context, filterContext.Exception, System.Configuration.ConfigurationManager.AppSettings["ErrorNotificationAddress"], false);
        //            this.View("Error").ExecuteResult(this.ControllerContext);
        //        }
        //    }
        //}
    }
}

